package com.ctsi.ssdc.admin.repository;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

import com.ctsi.ssdc.admin.domain.CscpMenus;
import com.ctsi.ssdc.admin.domain.CscpMenusExample;
import com.ctsi.ssdc.admin.domain.dto.CscpMenusDTO;
import com.ctsi.ssdc.database.annotation.InjectByDataBaseType;
import com.ctsi.ssdc.database.enums.EnumDatabaseName;

/**
 * @author MyBatis Generator
*/
@InjectByDataBaseType(includes= {EnumDatabaseName.ORACLE})
public interface CscpMenusRepository {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_menus
     *
     * @mbg.generated Tue Aug 28 09:53:39 CST 2018
     */
    @SelectProvider(type=CscpMenusSqlProvider.class, method="countByExample")
    long countByExample(CscpMenusExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_menus
     *
     * @mbg.generated Tue Aug 28 09:53:39 CST 2018
     */
    @DeleteProvider(type=CscpMenusSqlProvider.class, method="deleteByExample")
    int deleteByExample(CscpMenusExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_menus
     *
     * @mbg.generated Tue Aug 28 09:53:39 CST 2018
     */
    @Delete({
        "delete from cscp_menus",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_menus
     *
     * @mbg.generated Tue Aug 28 09:53:39 CST 2018
     */
    @Insert({
        "insert into cscp_menus (name, icon, ",
        "title, url, http_method, ",
        "component, parent_id, ",
        "type, permission_code, ",
        "orderby)",
        "values (#{name,jdbcType=VARCHAR}, #{icon,jdbcType=VARCHAR}, ",
        "#{title,jdbcType=VARCHAR}, #{url,jdbcType=VARCHAR}, #{httpMethod,jdbcType=VARCHAR}, ",
        "#{component,jdbcType=VARCHAR}, #{parentId,jdbcType=INTEGER}, ",
        "#{type,jdbcType=VARCHAR}, #{permissionCode,jdbcType=VARCHAR}, ",
        "#{orderby,jdbcType=INTEGER})"
    })
    @Options(useGeneratedKeys=true, keyProperty="id")
    int insert(CscpMenus record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_menus
     *
     * @mbg.generated Tue Aug 28 09:53:39 CST 2018
     */
    @InsertProvider(type=CscpMenusSqlProvider.class, method="insertSelective")
    @Options(useGeneratedKeys=true, keyProperty="id")
    int insertSelective(CscpMenus record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_menus
     *
     * @mbg.generated Tue Aug 28 09:53:39 CST 2018
     */
    @SelectProvider(type=CscpMenusSqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="icon", property="icon", jdbcType=JdbcType.VARCHAR),
        @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
        @Result(column="url", property="url", jdbcType=JdbcType.VARCHAR),
        @Result(column="http_method", property="httpMethod", jdbcType=JdbcType.VARCHAR),
        @Result(column="component", property="component", jdbcType=JdbcType.VARCHAR),
        @Result(column="parent_id", property="parentId", jdbcType=JdbcType.INTEGER),
        @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
        @Result(column="permission_code", property="permissionCode", jdbcType=JdbcType.VARCHAR),
        @Result(column="orderby", property="orderby", jdbcType=JdbcType.INTEGER)
    })
    List<CscpMenus> selectByExample(CscpMenusExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_menus
     *
     * @mbg.generated Tue Aug 28 09:53:39 CST 2018
     */
    @Select({
        "select",
        "id, name, icon, title, url, http_method, component, parent_id, type, permission_code, ",
        "orderby",
        "from cscp_menus",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="icon", property="icon", jdbcType=JdbcType.VARCHAR),
        @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
        @Result(column="url", property="url", jdbcType=JdbcType.VARCHAR),
        @Result(column="http_method", property="httpMethod", jdbcType=JdbcType.VARCHAR),
        @Result(column="component", property="component", jdbcType=JdbcType.VARCHAR),
        @Result(column="parent_id", property="parentId", jdbcType=JdbcType.INTEGER),
        @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
        @Result(column="permission_code", property="permissionCode", jdbcType=JdbcType.VARCHAR),
        @Result(column="orderby", property="orderby", jdbcType=JdbcType.INTEGER)
    })
    CscpMenus selectByPrimaryKey(Integer id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_menus
     *
     * @mbg.generated Tue Aug 28 09:53:39 CST 2018
     */
    @UpdateProvider(type=CscpMenusSqlProvider.class, method="updateByExampleSelective")
    int updateByExampleSelective(@Param("record") CscpMenus record, @Param("example") CscpMenusExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_menus
     *
     * @mbg.generated Tue Aug 28 09:53:39 CST 2018
     */
    @UpdateProvider(type=CscpMenusSqlProvider.class, method="updateByExample")
    int updateByExample(@Param("record") CscpMenus record, @Param("example") CscpMenusExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_menus
     *
     * @mbg.generated Tue Aug 28 09:53:39 CST 2018
     */
    @UpdateProvider(type=CscpMenusSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(CscpMenus record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_menus
     *
     * @mbg.generated Tue Aug 28 09:53:39 CST 2018
     */
    @Update({
        "update cscp_menus",
        "set name = #{name,jdbcType=VARCHAR},",
          "icon = #{icon,jdbcType=VARCHAR},",
          "title = #{title,jdbcType=VARCHAR},",
          "url = #{url,jdbcType=VARCHAR},",
          "http_method = #{httpMethod,jdbcType=VARCHAR},",
          "component = #{component,jdbcType=VARCHAR},",
          "parent_id = #{parentId,jdbcType=INTEGER},",
          "type = #{type,jdbcType=VARCHAR},",
          "permission_code = #{permissionCode,jdbcType=VARCHAR},",
          "orderby = #{orderby,jdbcType=INTEGER}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(CscpMenus record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_menus
     *
     * @mbg.generated Tue Aug 28 09:53:39 CST 2018
     */
    @SelectProvider(type=CscpMenusSqlProvider.class, method="selectByExampleWithPage")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="icon", property="icon", jdbcType=JdbcType.VARCHAR),
        @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
        @Result(column="url", property="url", jdbcType=JdbcType.VARCHAR),
        @Result(column="http_method", property="httpMethod", jdbcType=JdbcType.VARCHAR),
        @Result(column="component", property="component", jdbcType=JdbcType.VARCHAR),
        @Result(column="parent_id", property="parentId", jdbcType=JdbcType.INTEGER),
        @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
        @Result(column="permission_code", property="permissionCode", jdbcType=JdbcType.VARCHAR),
        @Result(column="orderby", property="orderby", jdbcType=JdbcType.INTEGER)
    })
    List<CscpMenus> selectByExamplewithPage(CscpMenusExample example);

    @Select({
            "select DISTINCT cm.*  ",
            "FROM cscp_menus cm  ",
            "left join cscp_role_menu crm on cm.id = crm.menu_id  ",
            "left join cscp_user_role cur on crm.role_id = cur.role_id  ",
            "where cur.user_id=#{userId,jdbcType=INTEGER}  ",
            "order by orderby"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="icon", property="icon", jdbcType=JdbcType.VARCHAR),
        @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
        @Result(column="url", property="url", jdbcType=JdbcType.VARCHAR),
        @Result(column="http_method", property="httpMethod", jdbcType=JdbcType.VARCHAR),
        @Result(column="component", property="component", jdbcType=JdbcType.VARCHAR),
        @Result(column="parent_id", property="parentId", jdbcType=JdbcType.INTEGER),
        @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
        @Result(column="permission_code", property="permissionCode", jdbcType=JdbcType.VARCHAR),
        @Result(column="orderby", property="orderby", jdbcType=JdbcType.INTEGER)
    })
    List<CscpMenus> selectByUserId(Integer userId);
    
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="icon", property="icon", jdbcType=JdbcType.VARCHAR),
        @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
        @Result(column="url", property="url", jdbcType=JdbcType.VARCHAR),
        @Result(column="parent_id", property="parentId", jdbcType=JdbcType.INTEGER),
        @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
        @Result(column="orderby", property="orderby", jdbcType=JdbcType.INTEGER),
        @Result(column="component", property="component", jdbcType=JdbcType.VARCHAR)
    })
    @Select({"select distinct c.id, c.title, c.icon, c.url, c.parent_id, c.type, c.orderby, "
    		+"c.component  FROM cscp_role_menu b, cscp_menus c "
    		+"where b.menu_id = c.id and b.role_id = #{roleId,jdbcType=INTEGER} order by c.orderby"})
    @MapKey("id")
    Map<Integer, CscpMenusDTO> menusByRole(Integer roleId);

    //按照父id搜索
    @Select({"select * from  cscp_menus where  parent_id = #{parentId,jdbcType=INTEGER} order by orderby"})
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="icon", property="icon", jdbcType=JdbcType.VARCHAR),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="url", property="url", jdbcType=JdbcType.VARCHAR),
            @Result(column="http_method", property="httpMethod", jdbcType=JdbcType.VARCHAR),
            @Result(column="component", property="component", jdbcType=JdbcType.VARCHAR),
            @Result(column="parent_id", property="parentId", jdbcType=JdbcType.INTEGER),
            @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
            @Result(column="permission_code", property="permissionCode", jdbcType=JdbcType.VARCHAR),
            @Result(column="orderby", property="orderby", jdbcType=JdbcType.INTEGER)
    })
    List<CscpMenus> selectByParentId(Integer parentId);
}